#ifndef __BASE_LOGGER_H_
#define __BASE_LOGGER_H_

#include <sstream>
#include <iostream>

#include "FileLog.h"
#include "Singleton.h"
#include "Time.h"

namespace BASE{
	class CLogger : public FileLog, public Singleton<CLogger>{
		public:
			CLogger():isInit(false){
			}

			~CLogger(){

			}

			bool isInitizlize() {
				return isInit;
			}

			bool openLog(const std::string & fileName, const std::string & pipeName = "", LOG_LEVEL level = LEVEL_WARN){
				/*std::string dirName = "/home/public/wuyaobo/Git/net/libbase";
				std::string fileName = dirName + "/loggerTest.txt";
				std::string pipeName = dirName + "/fifoTest.fifo";
				bool flag = initialize(fileName, pipeName, LEVEL_DEBUG);
				if (!flag){
					std::cerr << "initizlize log error" << std::endl;
				}*/
				bool flag = initialize(fileName, pipeName, level);
				if (!flag){
					std::cerr << "initialize log error" << std::endl;
					isInit = false;
					return false;
				}
				isInit = true;
				return true;
			}
		private:
			bool isInit;
	};
};

#define WRITE_LOG(level, fmt)								\
	do{														\
		BASE::CLogger * logger = BASE::CLogger::get();		\
		std::ostringstream ss;								\
		ss << logger->getLevel(level) << " "				\
			<< pthread_self() << " " 						\
			<< Time::getDateText() << " "					\ 
			<< __FILE__ << ":"<< __LINE__					\
			<< " " << fmt;									\
		if (logger->isInitizlize()){						\
			(*logger) << level << ss.str() << std::endl;	\
		}else {												\
			std::cout << ss.str() << std::endl;				\
		}													\
	}while(0)												\

#define DebugLog(fmt)		WRITE_LOG(BASE::LEVEL_DEBUG, fmt)
#define InfoLog(fmt)		WRITE_LOG(BASE::LEVEL_INFO, fmt) 
#define WarnLog(fmt)		WRITE_LOG(BASE::LEVEL_WARN, fmt)
#define ErrLog(fmt)			WRITE_LOG(BASE::LEVEL_ERROR, fmt)


#endif
